package com.ls.modules.designpattern.Prototype;

import java.util.HashMap;
import java.util.Map;


/**
 * @see 原型模式:实现复制粘贴功能-->复制一个文件到另外一个地方
 * @apiNote  核心思想:首先创建一个实例,然后通过这个实例去拷贝创建新的实例
 */
public class Prototype implements Cloneable{
    private Integer fileId;
    private String fileName;
    private Map<String,Double> scores;

    public Prototype(Integer fileId, String fileName, Map<String, Double> scores) {
        System.out.println("Prototype初始化构造方法");
        this.fileId = fileId;
        this.fileName = fileName;
        this.scores = scores;
    }

    public Integer getFileId() {
        return fileId;
    }

    public void setFileId(Integer fileId) {
        this.fileId = fileId;
    }

    public String getFileName() {
        return fileName;
    }

    public void setFileName(String fileName) {
        this.fileName = fileName;
    }

    public Map<String, Double> getScores() {
        return scores;
    }

    public void setScores(Map<String, Double> scores) {
        this.scores = scores;
    }

    @Override
    protected Prototype clone() {
        Prototype filePrototype = null;
        try {
            //有了下面这句话，基本类型就能克隆了
            filePrototype = (Prototype) super.clone();
            //下面要对每一个复杂对象进行分别克隆
            filePrototype.scores = (Map<String, Double>) ((HashMap)this.scores).clone();
        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
        }
        return filePrototype;
    }
}
